#include<stdbool.h>
#include<stdio.h>
#include "Stack.h"




void Stack_Init_OPTR(OPTR* ptr) //初始化运算符栈
{
	ptr->top = -1;
}

void Stack_Init_OPND(OPND* ptr) //初始化操作数栈
{
	ptr->top = -1;
}

bool JUdge_Stack_OPTR(OPTR* ptr)//判断操作符栈是否空
{
	if (ptr->top == -1)
	{
		return STACK_EMPTY;
	}
	else
		return 0;
}

bool JUdge_Stack_OPND(OPND* ptr)//判断数据栈是否空
{
	if (ptr->top == -1)
	{
		return STACK_EMPTY;
	}
	else
		return 0;
}


void Stack_Push_OPTR(OPTR* stack, char new_element) //操作符入栈
{
	if (stack->top < STACK_SIZE - 1)
	{
		(stack->top)++;
 		stack->element[stack->top] = new_element;
	}
	else
		printf("ERROR Push");

}

void Stack_Push_OPND(OPND* stack, int new_element) //数据入栈
{
	if (stack->top < STACK_SIZE - 1)
	{
		(stack->top)++;
		stack->element[stack->top] = new_element;
	}
	else
		printf("ERROR Push");
}

char Stack_Pop_OPTR(OPTR* stack) //从操作符栈顶取出一个元素
{
	char data;
	data = stack->element[stack->top];
	(stack->top)--;
	return data;
}

int Stack_Pop_OPND(OPND* stack) //从数据栈顶取出一个元素
{
	int data;
	data = stack->element[stack->top];
	(stack->top)--;
	return data;
}


int getIndex(char theta) //获取theta所对应的索引
{
	
	int index = 0;
	switch (theta)
	{
		case '+':
			index = 0;
			break;
		case '-':
			index = 1;
			break;
		case '*':
			index = 2;
			break;
		case '/':
			index = 3;
			break;
		case '(':
			index = 4;
			break;
		case ')':
		 index = 5;
		 break;
	 case '#':
		 index = 6;
	 default:break;
			    }
	 return index;
	}

char getPriority(char theta1, char theta2)
 //获取theta1与theta2之间的优先级
{
	 const char priority[][7] =
		//算符间的优先级关系
	 {
		 { '>', '>', '<', '<', '<', '<', '>' },
		 { '>', '>', '<', '<', '<', '<', '>' },
		 { '>', '>', '>', '>', '<', '<', '>' },
		 { '>', '>', '>', '>', '<', '<', '>' },
		 { '<', '<', '<', '<', '<', '=', '0' },
		 { '>', '>', '>', '>', '0', '>', '>' },
		 { '<', '<', '<', '<', '<', '0', '=' },
		};
	
	int index1 = getIndex(theta1);
	int index2 = getIndex(theta2);
	 return priority[index1][index2];
}
